// ============================================================================
//
// LCP-net framework by Pierre CHATEL chatelp@gmail.com
// Distributed under GPLv3
// Copyright (C) THALES Land & Joint Systems, 2008, 2009. All rights reserved
//
// ============================================================================

/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl;

import com.thalesgroup.preferenceFramework.models.Arc;
import com.thalesgroup.preferenceFramework.models.Node;

import com.thalesgroup.preferenceFramework.models.LCPnet.LCPnet;
import com.thalesgroup.preferenceFramework.models.LCPnet.LNode;
import com.thalesgroup.preferenceFramework.models.LCPnet.exceptions.LCPnetCombinationException;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.ArcFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.Fragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.FragmentsPackage;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LCPnetFragment;
import com.thalesgroup.preferenceFramework.models.LCPnet.fragments.LNodeFragment;

import com.thalesgroup.preferenceFramework.models.impl.ArcImpl;

import org.eclipse.emf.common.notify.Notification;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.impl.ENotificationImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Arc Fragment</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.ArcFragmentImpl#getFragmentName <em>Fragment Name</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.ArcFragmentImpl#isAntiFragment <em>Anti Fragment</em>}</li>
 *   <li>{@link com.thalesgroup.preferenceFramework.models.LCPnet.fragments.impl.ArcFragmentImpl#getBaseArc <em>Base Arc</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class ArcFragmentImpl extends ArcImpl implements ArcFragment {
	/**
	 * The default value of the '{@link #getFragmentName() <em>Fragment Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFragmentName()
	 * @generated
	 * @ordered
	 */
	protected static final String FRAGMENT_NAME_EDEFAULT = "";
	/**
	 * The cached value of the '{@link #getFragmentName() <em>Fragment Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getFragmentName()
	 * @generated
	 * @ordered
	 */
	protected String fragmentName = FRAGMENT_NAME_EDEFAULT;
	/**
	 * The default value of the '{@link #isAntiFragment() <em>Anti Fragment</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAntiFragment()
	 * @generated
	 * @ordered
	 */
	protected static final boolean ANTI_FRAGMENT_EDEFAULT = false;
	/**
	 * The cached value of the '{@link #isAntiFragment() <em>Anti Fragment</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isAntiFragment()
	 * @generated
	 * @ordered
	 */
	protected boolean antiFragment = ANTI_FRAGMENT_EDEFAULT;
	/**
	 * The cached value of the '{@link #getBaseArc() <em>Base Arc</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getBaseArc()
	 * @generated
	 * @ordered
	 */
	protected Arc baseArc;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected ArcFragmentImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return FragmentsPackage.Literals.ARC_FRAGMENT;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public String getFragmentName() {
		return fragmentName;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setFragmentName(String newFragmentName) {
		String oldFragmentName = fragmentName;
		fragmentName = newFragmentName;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.ARC_FRAGMENT__FRAGMENT_NAME, oldFragmentName, fragmentName));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public boolean isAntiFragment() {
		return antiFragment;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setAntiFragment(boolean newAntiFragment) {
		boolean oldAntiFragment = antiFragment;
		antiFragment = newAntiFragment;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.ARC_FRAGMENT__ANTI_FRAGMENT, oldAntiFragment, antiFragment));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Arc getBaseArc() {
		if (baseArc != null && baseArc.eIsProxy()) {
			InternalEObject oldBaseArc = (InternalEObject)baseArc;
			baseArc = (Arc)eResolveProxy(oldBaseArc);
			if (baseArc != oldBaseArc) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, FragmentsPackage.ARC_FRAGMENT__BASE_ARC, oldBaseArc, baseArc));
			}
		}
		return baseArc;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Arc basicGetBaseArc() {
		return baseArc;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setBaseArc(Arc newBaseArc) {
		Arc oldBaseArc = baseArc;
		baseArc = newBaseArc;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, FragmentsPackage.ARC_FRAGMENT__BASE_ARC, oldBaseArc, baseArc));
	}
	
	public void applyFragment(EObject context) throws LCPnetCombinationException {
		Arc baseArc = this.getBaseArc();
		
		if(context instanceof LNodeFragment) {
			//Fragment not contained in context --> remove link from context or resolve indirection

			LNodeFragment callerLNodeFragment = (LNodeFragment) context;

			if(this.isAntiFragment()) {
				if(callerLNodeFragment.getInArcs().contains(this)) {
					callerLNodeFragment.getBaseLNode().getInArcs().remove(baseArc);
				} else if(callerLNodeFragment.getOutArcs().contains(this)) {
					callerLNodeFragment.getBaseLNode().getOutArcs().remove(baseArc);
				} else {
					throw new LCPnetCombinationException(
							"Fragment consistancy error: the calling LNodeFragment is not related to ArcFragment "+ this.getName());
				}
				return;
			}
			
			//Warning: If it's an antiFragment, the following code will not be executed
						
			//Is the caller a start or end node of this fragment ?
			if(this.getStartNode().equals(callerLNodeFragment)) {
				baseArc.setStartNode(callerLNodeFragment.getBaseLNode());
			} else if(this.getEndNode().equals(callerLNodeFragment)) {
				baseArc.setEndNode(callerLNodeFragment.getBaseLNode());
			} else {
				throw new LCPnetCombinationException(
						"Fragment consistancy error: the calling LNodeFragment is not related to ArcFragment "+ this.getName());
			}
			
		} else if(context instanceof LCPnetFragment) {
			//Fragment contained in context --> apply fragment

			LCPnetFragment callingLCPNetFragment = (LCPnetFragment) context;
			
			if(this.isAntiFragment()) {
				LCPnet baseLCPnet = (LCPnet) callingLCPNetFragment.getBaseLCPnet();
				if(! baseLCPnet.getArcs().remove(baseArc)) {
					throw new LCPnetCombinationException(
							"Fragment consistancy error: this LCPNet ("+
							baseLCPnet.getName()+
							") does not contains the specified Arc ("+
							baseArc+
							")");
				}
				return;
			}
			
			//Warning: If it's an antiFragment, the following code will not be executed
			
			if(this.getName() != null && !this.getName().equals("")) {
				baseArc.setName(this.getName());
			}
			
			Node startNode = this.getStartNode();
			if(		startNode != null &&
					!(startNode instanceof Fragment) &&
					startNode instanceof LNode) {
				baseArc.setStartNode(startNode);
			}
			
			Node endNode = this.getEndNode();
			if(		endNode != null &&
					!(endNode instanceof Fragment) &&
					endNode instanceof LNode) {
				baseArc.setEndNode(endNode);
			}

		} else {
			throw new LCPnetCombinationException("Combination error: unexpected calling context for Arc fragment");
		}
		
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case FragmentsPackage.ARC_FRAGMENT__FRAGMENT_NAME:
				return getFragmentName();
			case FragmentsPackage.ARC_FRAGMENT__ANTI_FRAGMENT:
				return isAntiFragment() ? Boolean.TRUE : Boolean.FALSE;
			case FragmentsPackage.ARC_FRAGMENT__BASE_ARC:
				if (resolve) return getBaseArc();
				return basicGetBaseArc();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case FragmentsPackage.ARC_FRAGMENT__FRAGMENT_NAME:
				setFragmentName((String)newValue);
				return;
			case FragmentsPackage.ARC_FRAGMENT__ANTI_FRAGMENT:
				setAntiFragment(((Boolean)newValue).booleanValue());
				return;
			case FragmentsPackage.ARC_FRAGMENT__BASE_ARC:
				setBaseArc((Arc)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case FragmentsPackage.ARC_FRAGMENT__FRAGMENT_NAME:
				setFragmentName(FRAGMENT_NAME_EDEFAULT);
				return;
			case FragmentsPackage.ARC_FRAGMENT__ANTI_FRAGMENT:
				setAntiFragment(ANTI_FRAGMENT_EDEFAULT);
				return;
			case FragmentsPackage.ARC_FRAGMENT__BASE_ARC:
				setBaseArc((Arc)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case FragmentsPackage.ARC_FRAGMENT__FRAGMENT_NAME:
				return FRAGMENT_NAME_EDEFAULT == null ? fragmentName != null : !FRAGMENT_NAME_EDEFAULT.equals(fragmentName);
			case FragmentsPackage.ARC_FRAGMENT__ANTI_FRAGMENT:
				return antiFragment != ANTI_FRAGMENT_EDEFAULT;
			case FragmentsPackage.ARC_FRAGMENT__BASE_ARC:
				return baseArc != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eBaseStructuralFeatureID(int derivedFeatureID, Class<?> baseClass) {
		if (baseClass == Fragment.class) {
			switch (derivedFeatureID) {
				case FragmentsPackage.ARC_FRAGMENT__FRAGMENT_NAME: return FragmentsPackage.FRAGMENT__FRAGMENT_NAME;
				case FragmentsPackage.ARC_FRAGMENT__ANTI_FRAGMENT: return FragmentsPackage.FRAGMENT__ANTI_FRAGMENT;
				default: return -1;
			}
		}
		return super.eBaseStructuralFeatureID(derivedFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public int eDerivedStructuralFeatureID(int baseFeatureID, Class<?> baseClass) {
		if (baseClass == Fragment.class) {
			switch (baseFeatureID) {
				case FragmentsPackage.FRAGMENT__FRAGMENT_NAME: return FragmentsPackage.ARC_FRAGMENT__FRAGMENT_NAME;
				case FragmentsPackage.FRAGMENT__ANTI_FRAGMENT: return FragmentsPackage.ARC_FRAGMENT__ANTI_FRAGMENT;
				default: return -1;
			}
		}
		return super.eDerivedStructuralFeatureID(baseFeatureID, baseClass);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (fragmentName: ");
		result.append(fragmentName);
		result.append(", antiFragment: ");
		result.append(antiFragment);
		result.append(')');
		return result.toString();
	}

} //ArcFragmentImpl
